// Javascript class to handle the events on each listbox item
function listBoxItem(target, index) {
	this.target = target;
	this.index = index;
	this.setCallback(this.target, "mouseEnter", this.handleMouseEnter);
	this.setCallback(this.target, "mouseLeave", this.handleMouseLeave);
	this.setCallback(this.target.findName("play" + index.toString()), "mouseLeftButtonUp", this.handlePlayClick);
    this.setCallback(this.target.findName("play" + index.toString()), "mouseLeftButtonDown", this.handlePlayMouseDown);
	this.setCallback(this.target.findName("play" + index.toString()), "mouseEnter", this.handlePlayEnter);
	this.setCallback(this.target.findName("play" + index.toString()), "mouseLeave", this.handlePlayLeave);
}

listBoxItem.prototype.handleMouseEnter = function(sender, eventArgs) {
	var itemBackground = this.target.findName("itemBackground" + this.index.toString());
	itemBackground.fill = "#FF222222";
	
	var pathBackground = this.target.findName("pathBackground" + this.index.toString());
	this.oldFill = pathBackground.fill;
	pathBackground.fill = "#FF222222";
}

listBoxItem.prototype.handleMouseLeave = function(sender, eventArgs) {
    var itemBackground = this.target.findName("itemBackground" + this.index.toString());
	itemBackground.fill = "black";
	
    var pathBackground = this.target.findName("pathBackground" + this.index.toString());
	pathBackground.fill = this.oldFill;
}

listBoxItem.prototype.handlePlayClick = function(sender, eventArgs) {
	// Add entry to the Atlas history manager
    histManager.addEntry(this.index + "");
    
    loadVideo(this.index);
    var scale = this.target.findName("scale" + this.index.toString())
    scale.scaleX = 1.2;
    scale.scaleY = 1.2;
}

listBoxItem.prototype.handlePlayMouseDown = function(sender, eventArgs) {
	var scale = this.target.findName("scale" + this.index.toString())
    scale.scaleX = .9;
    scale.scaleY = .9;
}

listBoxItem.prototype.handlePlayEnter = function(sender, eventArgs) {
    var scale = this.target.findName("scale" + this.index.toString())
    scale.scaleX = 1.2;
    scale.scaleY = 1.2;
}

listBoxItem.prototype.handlePlayLeave = function(sender, eventArgs) {
    var scale = this.target.findName("scale" + this.index.toString())
    scale.scaleX = 1;
    scale.scaleY = 1;
}

listBoxItem.prototype.setCallback = function(target, eventName, callback) {
	if (!window.methodIndex)
		window.methodIndex = 0;
	
	var callbackName = "uniqueCallback" + (window.methodIndex++);
	var controller = this;
	var func = function() {
		callback.apply(controller, arguments);
	}
	
	eval(callbackName + " = func;");
	target[eventName] = "javascript:" + callbackName;
}
